<?php
namespace Admin\Controller;
use Think\Controller;
use Myself\FileSystem;
use Admin\Controller\CommonController;
class CustomController extends CommonController { 

	
	
	protected $std_level = array(0=>'公告',1=> '消息');
	

    public function index(){
		header('Content-Type:text/html;charset=utf-8');
		
		//-----------------搜索-------------------
		if(!empty(I('select_name5'))){
		   $where['cus_prv']=I('select_name5');
		}
		if(!empty(I('select_name6'))){
		   $where['cus_city']=I('select_name6');
		}
		if(!empty(I('select_name7'))){
		   $where['cus_school']=I('select_name7');
		}
		
		
	        $list_address= M('shop_address')->where('level=1')->select(); 
		$sh_q = I('sh_q','');//关键字
		if(!empty($sh_q)){
			$shArray['sh_q'] = $sh_q;
			$map['phone'] = array('LIKE','%'.$sh_q.'%');
			$map['_logic'] = 'OR';
			$where['_complex'] = $map;
		}


		 $from_num = I('from_num', '');
                 $to_num = I('to_num', '');
                
          
		 if($from_num){
			//$from_time_s = strtotime($from_time);
			$where['ordernum']= array('EGT',$from_num);
			$shArray['from_num'] = $from_num;
		 }
		
                
		 if($to_num){
			//$to_time_s = strtotime($to_time)+86400;
			$where['ordernum']= array('ELT',$to_num);
			$shArray['to_num'] = $to_num;	
		 }	
                
		
		if($from_num && $to_num){
			$where['ordernum']=array(array('EGT',$from_num),array('ELT',$to_num),'and');
		}
                
                
                
			 
                $from_money = I('from_money', '');
                $to_money = I('to_money', '');
                
        
                  
		if($from_money){
			//$from_time_s = strtotime($from_time);
			$where['monetary']= array('EGT',$from_money);
			$shArray['from_money'] = $from_money;
		}
		
		if($to_money){
			//$to_time_s = strtotime($to_time)+86400;
			$where['monetary']= array('ELT',$to_money);
			$shArray['to_money'] = $to_money;	
		}		
		
		if($from_money && $to_money){
			$where['monetary']=array(array('EGT',$from_money),array('ELT',$to_money),'and');
		}
                
              
                $std_level = I('std_level','');
                if($std_level){
                   
			$where['std_level']= $std_level;
			$shArray['std_level'] = $std_level;
		}
               
                
                $sex = I('sex','');
                if($sex){
                    
                
                        $where['sex']= $sex;
			$shArray['sex'] = $sex;
		}
          
                
                
		if(!empty($shArray)){$this->assign('shArray',$shArray);}// 搜索参数输出
		//-------------------------搜索end-----------------

		$pageno = I('p',1);
                $this->assign('pageno',$pageno);// 赋值数据集
		$pageCount = 10;
		$list = M('customer')->where($where)->order('id desc, add_time desc')->page($pageno.','.$pageCount)->select();
		foreach( $list as $key => $item){
		
		$list[$key]['fullname'] = M('customer_detail')->where('c_id="'.$item['id'].'"')->getField('fullname');
		//$list[$key]['phone'] = M('member')->where('id="'.$item['cid'].'"')->getField('phone');
		//$list[$key]['sex'] = M('customer_detail')->where('c_id="'.$item['id'].'"')->getField('sex');
		//$list[$key]['monetary'] = M('customer_detail')->where('c_id="'.$item['id'].'"')->getField('monetary');
		//$list[$key]['ordernum'] = M('customer_detail')->where('c_id="'.$item['id'].'"')->getField('ordernum');
		$list[$key]['provin_name'] = M('shop_address')->where('id="'.$item['cus_prv'].'"')->getField('name');
		$list[$key]['city_name'] = M('shop_address')->where('id="'.$item['cus_city'].'"')->getField('name');
		$list[$key]['school_name'] = M('shop_address')->where('id="'.$item['cus_school'].'"')->getField('name');
		//$list[$key]['std_level'] = M('customer_detail')->where('c_id="'.$item['id'].'"')->getField('std_level');
		$list[$key]['jobwant_num'] = M('customer_jobwant')->where('cid="'.$item['cid'].'"')->count();
		$list[$key]['oldthing_num'] = M('customer_oldthing')->where('cid="'.$item['cid'].'"')->count();
                $list[$key]['lastorder_time'] = M('shop_product_order')->where('cid="'.$item['cid'].'"')->getField('add_time');
		}
		
		
		$this->assign('list',$list);
		$count = M('customer')->where($where)->count();
		$this->assign('count',$count);
		$Page = new \Think\Page($count,$pageCount);
		$show = $Page->showAdmin();
		$this->assign('page',$show);
                $this->assign('list1',$list_one);
		$std_level  = array(1=>'一年级',2=>'二年级',3=>'三年级',4=>'四年级');
                $sex  = array(1=>'女',2=>'男');
		$this->assign('std_level', $std_level);
                $this->assign('sex', $sex);
		$this->assign('listAddress',$list_address);
		$this->assign('statusArr',$this->statusArr);
		$this->display('Custom/index');
    }
	
    public function editStatus(){
	     $cid = I('cid');
             $status = I('state');
	     $dataInfo = M('customer')->find($cid);
		 
	   if( $dataInfo ){
	      if( $status == 1){
		     $data['status'] = 0;
		   }else{
		      $data['status'] = 1;
		   }
		   $result = M('customer')->where('id="'.$dataInfo['id'].'"')->save($data);
	   }
		 
	     if( $result ){
		 $info = 100;
		 $this->ajaxReturn($info);
		 }
	 
	 }
	 
  public  function edit(){
 
	  $id = I('id');
	  $dataInfo = M('customer')->where('id="'.$id.'"')->find($id);
	
	  if( $dataInfo ){
		 $this->assign('dataInfo', $dataInfo);
		;
		 $this->display();
	  }

	}
	
  public  function  saveEdit(){
      	 $data['reply'] = I('reply');
	     $id = I('updateid');
		 if( !empty(I('reply')) ){
		 $data['reply_status'] = 1;
		  }else{
		  $data['reply_status'] = 0;
		  }
         if($data){
		  $result = M('customer')->where('id="'.$id.'"')->save($data);
		 }
		 
		 if($result){
		    $info['status'] =100;
		    $info['info'] = '修改成功';
				  $this->ajaxReturn($info);
		 }else{
		    $info['status'] =100;
		     $this->ajaxReturn($info);
		 }
       
	    

  }	
  
  
     public function add(){
		 $typeArr  = array(0=>'公告',1=>'消息');
		 $this->assign('typeArr', $typeArr);
		 $this->display();
    }
	
	
	public  function save(){
	     $data['title']= I('title');
         $data['type_id'] = I('colid');
         $data['content'] = I('content');
		 $data['author'] = $_SESSION['admin_user_id'];
		
	     if( $data ){
          $result = M('customer')->add($data);		 
		 }
	     
		 if( $result ){
		 $info['status'] = 100;
		 $info['info'] = '添加成功';
		 $this->ajaxReturn($info);
		 
		 }
  }
      public function del(){

            $id = $_REQUEST['id'];
			
            if (is_array($id)){
               $where = 'id in('.implode(',', $id).')';
            } else {
               $where = 'id='.$id;
            }
			
            $list = M('customer')->where($where)->delete();
			
			
		   $this->redirect('Custom/index');
    }

	
	 
}